import java.io.IOException;

/**
 * @Description:
 * @User: Administrator
 * @Date: 2018/7/30 0030
 */
public class Adb {

    String adbPath = "adb";
    String packageName = "cn.firegod.study.myapplication";
    String activityName = "MainActivity";
    String app = "D:\\ebplayer-3.9.45.apk";

    enum Command {
        CONNECT, INSTALL_APP, UNINSTALL_APP, REBOOT, START_APP, STOP_APP, CLEAN_APP, WAIT
    }

    private void connect(String host) {
        String cmd = adbPath + " connect " + host;
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void install(String host, String apkCkpFile) {
        String cmd = adbPath + " -s " + host + " install " + apkCkpFile;
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void uninstall(String host, String packageName) {
        String cmd = adbPath + " -s " + host + " uninstall " + packageName;
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void start(String host, String packageName, String activity) {
        String cmd = adbPath + " -s " + host + " shell am start  " + packageName + "/." + activity;
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void stop(String host, String packageName) {
        String cmd = adbPath + " -s " + host + " shell am force-stop  " + packageName;
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void clearData(String host, String packageName) {
        String cmd = adbPath + " -s " + host + " shell pm clear " + packageName;
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void reboot(String host) {
        String cmd = adbPath + " -s " + host + " reboot";
        try {
            Process exec = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 一个 host 执行多个 cmd
     *
     * @param host
     * @param cmds
     */
    private void batchCmd(String host, Command... cmds) {
        for (Command cmd : cmds) {
            executeCmd(cmd, host);
        }
    }

    /**
     * 多个 host 执行一个 cmd
     *
     * @param cmd
     * @param hosts
     */
    private void batchHost(Command cmd, String... hosts) {
        for (String host : hosts) {
            executeCmd(cmd, host);
        }
    }

    private void executeCmd(Command cmd, String host) {
        switch (cmd) {
            case CONNECT:
                //连接
                this.connect(host);
                break;
            case STOP_APP:
                this.stop(host, packageName);
                break;
            case CLEAN_APP:
                this.clearData(host, packageName);
                break;
            case UNINSTALL_APP:
                this.uninstall(host, packageName);
                break;
            case INSTALL_APP:
                this.install(host, app);
                break;
            case START_APP:
                this.start(host, packageName, activityName);
                break;
            case REBOOT:
                this.reboot(host);
                break;
            case WAIT:
                this.sleep(2000);
                break;
            default:
                System.out.println("命令不认识");
        }
    }

    private void sleep(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {

        Adb adb = new Adb();

//        一楼柱子屏 6.10 - 6.20
        String[] layer_1_zhuzi = new String[]{
                "172.16.6.10",
                "172.16.6.11",
                "172.16.6.12",
                "172.16.6.13",
                "172.16.6.14",
                "172.16.6.15",
                "172.16.6.16",
                "172.16.6.17",
                "172.16.6.18",
                "172.16.6.19",
                "172.16.6.20"
        };
//        一楼窗口屏 7.10 - 7.42
        String[] layer_1_ck = new String[]{
                "172.16.7.10",
                "172.16.7.11",
                "172.16.7.12",
                "172.16.7.13",
                "172.16.7.14",
                "172.16.7.15",
                "172.16.7.16",
                "172.16.7.17",
                "172.16.7.18",
                "172.16.7.19",
                "172.16.7.20",
                "172.16.7.21",
                "172.16.7.22",
                "172.16.7.23",
                "172.16.7.24",
                "172.16.7.25",
                "172.16.7.26",
                "172.16.7.27",
                "172.16.7.28",
                "172.16.7.29",
                "172.16.7.30",
                "172.16.7.31",
                "172.16.7.32",
                "172.16.7.33",
                "172.16.7.34",
                "172.16.7.35",
                "172.16.7.36",
                "172.16.7.37",
                "172.16.7.38",
                "172.16.7.39",
                "172.16.7.40",
                "172.16.7.41",
                "172.16.7.42"
        };
//        三楼窗口屏 7.119 - 7.150
        String[] layer_3_ck = new String[]{
                "172.16.7.119",
                "172.16.7.120",
                "172.16.7.121",
                "172.16.7.122",
                "172.16.7.123",
                "172.16.7.124",
                "172.16.7.125",
                "172.16.7.126",
                "172.16.7.127",
                "172.16.7.128",
                "172.16.7.129",
                "172.16.7.130",
                "172.16.7.131",
                "172.16.7.132",
                "172.16.7.133",
                "172.16.7.134",
                "172.16.7.135",
                "172.16.7.136",
                "172.16.7.137",
                "172.16.7.138",
                "172.16.7.139",
                "172.16.7.140",
                "172.16.7.141",
                "172.16.7.142",
                "172.16.7.143",
                "172.16.7.144",
                "172.16.7.145",
                "172.16.7.146",
                "172.16.7.147",
                "172.16.7.148",
                "172.16.7.149",
                "172.16.7.150"
        };
//        三楼柱子屏 6.39 - 6.45
        String[] layer_3_zhuzi = new String[]{
                "172.16.6.39",
                "172.16.6.40",
                "172.16.6.41",
                "172.16.6.42",
                "172.16.6.43",
                "172.16.6.44",
                "172.16.6.45"
        };

        String[] temp = new String[]{
                "172.16.6.23",
                "172.16.6.27",
                "172.16.6.31",
                "172.16.6.35",
                "172.16.6.36",
                "172.16.6.47",
                "172.16.6.48",
                "172.16.6.49",
                "172.16.6.50"
        };

        String port = "55555";

//        String ipPrefix = "172.16.7";
//        int startIP = 10;
//        int stopIP = 42;
//        for (int j = startIP; j < stopIP + 1; j++) {
//            System.out.println(ipPrefix + "." + j + ":" + port);
//        }

        String executeHost[] = temp;
        for (String host : executeHost) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    adb.batchCmd(host + ":" + port, Command.CONNECT, Command.WAIT, Command.INSTALL_APP);
                }
            }).start();
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
